import DataColumn from '../dataColumn.js';
import DataColumnBoolean from '../dataColumnBoolean.js';
import DataColumnDate from '../dataColumnDate.js';
import DataColumnDateTime from '../dataColumnDateTime.js';
import DataColumnDecimal from '../dataColumnDecimal.js';
import DataColumnEnum from '../dataColumnEnum.js';
import DataColumnLong from '../dataColumnLong.js';
import DataColumnString from '../dataColumnString.js';
import DataColumnTime from '../dataColumnTime.js';
import DataSet from '../dataSet.js';
// import DataTable from '../dataTable.js';
import GeneralDataTable from '../generalDataTable.js';

export default class DataSetSerializerJSON {

	serializer(dataSet, config) {
		let dataSetJson = {};
		dataSetJson["id"] = dataSet.id;
		dataSetJson["dataTables"] = [];
		dataSetJson["id"] = dataSet.id
		dataSetJson["dataTables"] = []
		let dataTables = dataSet.dataTables;
		dataTables.forEach(dataTable => {
			let dataTableJson = {};
			if (dataTable instanceof GeneralDataTable) {
				dataTableJson = this.toJSONObject(dataTable, config);
			// } else if (dataTable instanceof PartitionDataTable) {
			// 	dataTableJson = toJSONObject((PartitionDataTable)dataTable, config);
			} else {
				alert("数据表"+dataTable.name+"不支持序列化：%s");
			}
			dataSetJson.dataTables.push(dataTableJson);
		})
		return JSON.stringify(dataSetJson);
	}
	
	toJSONObject(dataTable, config) {
		let dataTableJson = {};
		//Schema
		let schemaJSON = {};
		schemaJSON["id"] = dataTable.id;
		schemaJSON["name"] = dataTable.name;
		schemaJSON["label"] = dataTable.label;
		schemaJSON["type"] = "General";
		schemaJSON["columns"] = [];
		dataTable.dataColumns.forEach(dataColumn => {
			let columnJSON = this.toSimpleDataColumn(dataColumn);
			schemaJSON.columns.push(columnJSON);
		});
		dataTableJson["schema"] = schemaJSON;
		
		//Data
		let dataJSONArray = [];
		dataTable.getDataRows().forEach(dataRow => {
			
			let dataRowJSON = {};
			dataRow.getDataCells().forEach(dataCell => {
				dataRowJSON[dataCell.columnName] = dataCell.value;
			});
			dataJSONArray.push(dataRowJSON);
			
		});
		
		dataTableJson["data"] = dataJSONArray;
		
		return dataTableJson;
	}
	
	toSimpleDataColumn(dataColumn) {
		let columnJSON = {};
		columnJSON["name"] = dataColumn.name;
		columnJSON["label"] = dataColumn.label;
		columnJSON["visible"] = dataColumn.visible;
		columnJSON["helpText"] = dataColumn.helpText;
		columnJSON["displayLength"] = dataColumn.displayLength;
		columnJSON["alignment"] = dataColumn.alignment;
		
		if (dataColumn instanceof DataColumnString) {
			let dataColumnString = dataColumn;
			columnJSON["stringSize"] = dataColumnString.getStringSize();
			columnJSON["type"] = dataColumnString.getDataType();
		} else if (dataColumn instanceof DataColumnLong) {
			let dataColumnLong = dataColumn;
			columnJSON["showZero"] = dataColumnLong.isShowZero();
			columnJSON["thousandSeperator"] = dataColumnLong.isThousandSeperator();
			columnJSON["type"] = dataColumnLong.getDataType();
		} else if (dataColumn instanceof DataColumnDecimal) {
			let dataColumnDecimal = dataColumn;
			columnJSON["showZero"] = dataColumnDecimal.isShowZero();
			columnJSON["thousandSeperator"] = dataColumnDecimal.isThousandSeperator();
			columnJSON["numOfDecimals"] = dataColumnDecimal.getNumOfDecimals();
			columnJSON["displayDecimals"] = dataColumnDecimal.getDisplayDecimals();
			columnJSON["currencyType"] = dataColumnDecimal.currencyType;
			columnJSON["currencySymbol"] = dataColumnDecimal.currencySymbol;
			columnJSON["percentage"] =  dataColumnDecimal.isPercentage();
			columnJSON["type"] = dataColumnDecimal.getDataType();
		} else if (dataColumn instanceof DataColumnDate) {
			let dataColumnDate = dataColumn;
			columnJSON["displayFormat"] = dataColumnDate.getDisplayFormat();
			columnJSON["type"] = dataColumnDate.getDataType();
		} else if (dataColumn instanceof DataColumnTime) {
			let dataColumnDate = dataColumn;
			columnJSON["displayFormat"] = dataColumnDate.getDisplayFormat();
			columnJSON["type"] = dataColumnDate.getDataType();
		} else if (dataColumn instanceof DataColumnDateTime) {
			let dataColumnDate = dataColumn;
			columnJSON["displayFormat"] = dataColumnDate.getDisplayFormat();
			columnJSON["type"] = dataColumnDate.getDataType();
		} else if (dataColumn instanceof DataColumnBoolean) {
			let dataColumnDate = dataColumn;
			columnJSON["textTrue"] = dataColumnDate.getTextTrue();
			columnJSON["textFalse"] = dataColumnDate.getTextFalse();
			columnJSON["type"] = dataColumnDate.getDataType();
		} else if (dataColumn instanceof DataColumnEnum) {
			let dataColumnEnum = dataColumn;
			columnJSON["enumItems"] = [];
			dataColumnEnum.getEnumItems().forEach(enumItem=>{
				let enumItemJSON = {};
				enumItemJSON["name"] = enumItem.name;
				enumItemJSON["label"] = enumItem.label;
				enumItemJSON["value"] = enumItem.value;
				columnJSON.enumItems.push(enumItemJSON);
			});
			columnJSON["type"] = dataColumnEnum.getDataType();
		} else if (dataColumn instanceof DataColumnComponent) {
			let dataColumnComponent = dataColumn;
			columnJSON["arrayItemName"] = dataColumnComponent.getArrayItemName();
			columnJSON["stringSize"] = dataColumnComponent.getStringSize();
			columnJSON["type"] = dataColumnComponent.getDataType();
		}
		
		return columnJSON;
	}
	
	deserializer(serializerText) {
		// TODO 自动生成的方法存根
		return null;
	}

}